// 分页控件
function setupPagination(total, currentPage, pageSize) {
    const totalPages = Math.ceil(total / pageSize);
    const pagination = $("#pagination");
    pagination.empty();

    if (totalPages <= 1) {
        return; // 如果总页数小于等于1，不需要分页
    }

    // 上一页按钮
    pagination.append($("<button>").text("上一页").addClass("prev-btn").attr("disabled", currentPage === 1));

    const maxVisibleButtons = 5; // 最多显示的分页按钮数
    const ellipsisThreshold = 3; // 当总页数大于该阈值时显示省略号

    let startPage = Math.max(1, currentPage - Math.floor(maxVisibleButtons / 2));
    let endPage = Math.min(totalPages, currentPage + Math.floor(maxVisibleButtons / 2));

    if (currentPage - 1 <= Math.floor(maxVisibleButtons / 2)) {
        endPage = Math.min(totalPages, maxVisibleButtons);
    }

    if (totalPages - currentPage <= Math.floor(maxVisibleButtons / 2)) {
        startPage = Math.max(1, totalPages - maxVisibleButtons + 1);
    }

    if (startPage > 1) {
        pagination.append($("<button>").text("1").addClass("page-btn").data("page", 1));
        if (startPage > 2) {
            pagination.append($("<span>").text("...").addClass("ellipsis"));
        }
    }

    for (let i = startPage; i <= endPage; i++) {
        const button = $("<button>").text(i).addClass("page-btn").data("page", i);
        if (i === currentPage) {
            button.addClass("active");
        }
        pagination.append(button);
    }

    if (endPage < totalPages) {
        if (totalPages - endPage >= ellipsisThreshold) {
            pagination.append($("<span>").text("...").addClass("ellipsis"));
        }
        pagination.append($("<button>").text(totalPages).addClass("page-btn").data("page", totalPages));
    }

    // 下一页按钮
    pagination.append($("<button>").text("下一页").addClass("next-btn").attr("disabled", currentPage === totalPages));
}
